package com.siashan.unipay4j.ali;

import com.siashan.unipay4j.ali.conf.AliApp;
import com.siashan.unipay4j.ali.conf.AliConfigUtil;
import com.siashan.unipay4j.biz.PayBiz;
import com.siashan.unipay4j.biz.PayBizProperties;
import com.siashan.unipay4j.biz.PayBizUtil;
import com.siashan.unipay4j.core.exception.Unipay4jException;
import com.siashan.unipay4j.core.pay.INotify;
import com.siashan.unipay4j.core.pay.IPayment;
import com.siashan.unipay4j.core.pay.IQuery;
import com.siashan.unipay4j.core.pay.IRefund;
import com.siashan.unipay4j.core.transfer.ITransfer;

import java.util.Map;

/**
 * 支付宝工厂服务类
 *
 * @author  siashan
 * @since v1.0.1
 **/
public class AliPayFactoryProvider {
    private AliPayProperties aliPayProperties;

    private AliPayFactory aliPayFactory;

    private PayBizProperties payBizProperties;

    private Map<String, AliApp> appMap;
    private Map<String, PayBiz> bizMap;

    public AliPayFactoryProvider(AliPayProperties aliPayProperties, AliPayFactory aliPayFactory,PayBizProperties payBizProperties) {
        this.aliPayProperties = aliPayProperties;
        this.aliPayFactory = aliPayFactory;
        this.payBizProperties = payBizProperties;
        this.appMap = AliConfigUtil.transAppMap(this.aliPayProperties.getConfig().getApps());
        this.bizMap = PayBizUtil.transBizMap(this.payBizProperties.getBizs());
    }

    /**
     * 获取支付对象
     *
     * @param appCode 应用code
     * @param bizCode  业务code
     * @return  支付类
     */
    public IPayment getPayment(String appCode,String bizCode) {
        AliApp aliApp = getAliApp(appCode);
        return aliPayFactory.getPayment(aliApp.getTradeType(),initPayConfig(aliApp,PayBizUtil.getPayNotifyUrl(bizMap,bizCode)));
    }

    /**
     * 获取支付对象
     *
     * @param tradeType   交易类型
     * @param config      支付宝配置
     * @return  支付类
     */
    public IPayment getPayment(String tradeType,AliPayConfig config) {
        return aliPayFactory.getPayment(tradeType,config);
    }



    /**
     * 获取回调对象
     *
     * @return  回调类
     */
    public INotify getNotify(String appCode) {
        AliApp aliApp = getAliApp(appCode);
        return aliPayFactory.getNotify(initPayConfig(aliApp));
    }

    /**
     * 获取查询对象
     *
     * @return  查询类
     */
    public IQuery getQuery(String appCode, String bizCode) {
        AliApp aliApp = getAliApp(appCode);
        return aliPayFactory.getQuery(initPayConfig(aliApp,PayBizUtil.getPayNotifyUrl(bizMap,bizCode)));
    }

    /**
     * 获取转账对象
     *
     * @return  转账类
     */
    public ITransfer getTransfer(String appCode){
        AliApp aliApp = getAliApp(appCode);
        return aliPayFactory.getTransfer(initPayConfig(aliApp));
    }

    /**
     * 获取退款对象
     *
     * @return  退款类
     */
    public IRefund getRefund(String appCode,String bizCode){
        AliApp aliApp = getAliApp(appCode);
        return aliPayFactory.getRefund(initPayConfig(aliApp,PayBizUtil.getRefundNotifyUrl(bizMap,bizCode)));
    }


    private AliPayConfig initPayConfig() {
        return initPayConfig(null, null);
    }

    private AliPayConfig initPayConfig(AliApp aliApp) {
        return initPayConfig(aliApp, null);
    }

    private AliPayConfig initPayConfig(AliApp aliApp, String notifyUrl) {
        AliPayConfig aliPayConfig = new AliPayConfig();
        aliPayConfig.setAppId(aliApp.getAppId());
        aliPayConfig.setMerchantPrivateKey(aliApp.getMerchantPrivateKey());
        aliPayConfig.setAlipayPublicKey(aliApp.getAlipayPublicKey());
        aliPayConfig.setAppCertPath(aliApp.getAppCertPath());
        aliPayConfig.setAlipayCertPath(aliApp.getAlipayCertPath());
        aliPayConfig.setAlipayRootCertPath(aliApp.getAlipayRootCertPath());
        aliPayConfig.setNotifyUrl(notifyUrl);
        return aliPayConfig;
    }


    private AliApp getAliApp(String appCode) {
        AliApp aliApp = this.appMap.get(appCode);
        if (null == aliApp) {
            throw new Unipay4jException("无效的appCode，请检查应用配置");
        }
        return aliApp;
    }

}
